package com.whx.managea.controller;


import com.whx.managea.entity.Grade;
import com.whx.managea.entity.Result;
import com.whx.managea.entity.Student;
import com.whx.managea.entity.vo.GradeVo;
import com.whx.managea.service.GradeService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    Grade grade;

    //定义用来删除成绩
    @RequestMapping("deleteGrade")
    public String deleteGradeById(String gd) {
        //创建result对象
        Result rs =new Result();

        //获得删除对象的id
        Integer id =Integer.valueOf(gd);
        try {
            //验证获取的id是否为空，是否为大于0的数
            if(id>0){
                //执行删除
                System.out.println(id);
                gradeService.deleteGradeById(id);
                //返回信息
                System.out.println("删除成功");
                rs.setMessage("删除成功").setStatus(true);
            }
            throw new RuntimeException("不存在的成绩！");
        } catch (Exception e) {
            rs.setStatus(false).setMessage(e.getMessage());
        }
        return "redirect:/grade/listGrades";
    }




    @RequestMapping("change")
    @ResponseBody
    public int changeGrade(GradeVo gradeVo,String act){
        //System.out.println(gradeVo.toString());
        grade.setId(gradeVo.getId()).setClazzId(gradeVo.getClazzId()).setCourseName(gradeVo.getCourseName())
           .setGroupId(gradeVo.getGroupId()).setScore(gradeVo.getScore());
        int data= gradeService.changeGrade(grade);
        return  data;
    }




    @RequestMapping("addGrade")
    @ResponseBody
    public int addGrade(GradeVo gradeVo) {
       // Result res = new Result();
      int res=gradeService.save(new Grade().setStuId(gradeVo.getStuId()).setClazzId(gradeVo.getClazzId()).setCourseName(gradeVo.getCourseName())
                .setGroupId(gradeVo.getGroupId()).setScore(gradeVo.getScore()));
        return res;
    }

    @RequestMapping("serachGrade")
    public String searchGrade(Model model, @Valid String stuId,@Valid String courseName){
        model.addAttribute("grade",gradeService.getGrade(stuId,courseName));
        return "back/grade/grade";
    }

    @RequestMapping("listGrades")
    public String listGrades(Integer page,String searchCol,String searchValue,Model model){
        //1.获取用户查询的当前页
        page=page==null?1:page;
        //2.获取每页显示的条数
        Integer rows=2;

        //获取用户输入的条件
        System.out.println(searchCol+"----"+searchValue);
        //调用业务层
        //3.求每页显示的成绩列表
        List<GradeVo> grades = gradeService.listGrades(page,rows,searchCol,searchValue);
        //4.总记录数
        Integer counts =gradeService.counts(searchCol, searchValue);
        //5.总页数
        Integer pages=counts%rows==0?counts/rows:counts/rows+1;
        //2.设置在域中

        model.addAttribute("page", page);
        model.addAttribute("pages", pages);
        model.addAttribute("counts", counts);
        model.addAttribute("searchCol",searchCol);
        model.addAttribute("searchValue",searchValue);
        //3.转发到前端页面
        model.addAttribute("grades",gradeService.listGrades(page,rows,searchCol,searchValue));
        return "back/grade/grade";
    }




}
